<?php
/**
 * 手机端分类图片设置
 *
 *
 *
 * @好商城提供技术支持 授权请购买shopnc授权
 * @license    http://www.33hao.com
 * @link       交流群号：138182377
 */



defined('In33hao') or exit('Access Invalid!');
class sq_settingControl extends SystemControl{
    public function __construct(){
        parent::__construct();
        Language::read('mobile');
    }

    /**
     * 商圈设置
     */
    public function indexOp() {
        $sq_banner = Model('sq_setting');
        $bannerList = $sq_banner->getBannerList(array());
        foreach($bannerList as $key => $value){
              $bannerList[$key]['pic'] = UPLOAD_SITE_URL.'/'.ATTACH_MOBILE.'/sqbanner'.'/'.$value['images'];
        }

        Tpl::output('bannerlist',$bannerList);
        Tpl::setDirquna('mobile');
        Tpl::showpage('sq_setting.list');
    }

    /**
     * 商圈轮播图设置
     */
    public function sq_setting_addOp(){
        if ($_POST['form_submit'] == 'ok'){
            $sq_banner = Model('sq_setting');
            $count = $sq_banner->getBannerCount(array());
            if($count>=5){
                showMessage("轮播图只能设置5张",'index.php?act=sq_setting&op=index');
            }
            /**
             * 上传图片
             */
            if ($_FILES['link_pic']['name'] != ''){
                $upload = new UploadFile();
                $upload->set('default_dir',ATTACH_MOBILE.'/sqbanner');

                $result = $upload->upfile('link_pic');
                if ($result){
                    $_POST['link_pic'] = $upload->file_name;
                }else {
                    showMessage($upload->error);
                }
            }

            $inser_array = array();
            $inser_array['title'] = $_POST['banner_title'];
            $inser_array['images'] = $_POST['link_pic'];
            $inser_array['tags'] = $_POST['banner_tag'];
            $inser_array['links'] = $_POST['link'];

            $res = $sq_banner->addBanner($inser_array);
            if($res){
                showMessage("轮播图添加成功","index.php?act=sq_setting&op=index");
            }else{
                showMessage("轮播图添加失败");
            }
        }
        Tpl::setDirquna('mobile');
        Tpl::showpage('sq_banner.add');
    }

    /**
     * 商圈轮播图设置
     */
    public function sq_setting_editOp(){
        $sq_banner = Model('sq_setting');
        $tmp = $sq_banner->getBannerInfo(array('id'=>$_GET['id']));
        $tmp['pic'] = UPLOAD_SITE_URL.'/'.ATTACH_MOBILE.'/sqbanner'.'/'.$tmp['images'];
        if ($_POST['form_submit'] == 'ok'){
            $count = $sq_banner->getBannerCount(array());
            if($count>=5){
                showMessage("轮播图只能设置5张",'index.php?act=sq_setting&op=index');
            }
            $inser_array = array();

            /**
             * 上传图片
             */
            if ($_FILES['link_pic']['name'] != ''){
                $upload = new UploadFile();
                $upload->set('default_dir',ATTACH_MOBILE.'/sqbanner');
                $result = $upload->upfile('link_pic');
                if ($result){
                    $_POST['link_pic'] = $upload->file_name;
                    $inser_array['images'] = $_POST['link_pic'];
                }else {
                    showMessage($upload->error);
                }
            }

            $inser_array['title'] = $_POST['banner_title'];
            $inser_array['tags'] = $_POST['banner_tag'];
            $inser_array['links'] = $_POST['link'];
            $where['id'] = $_POST['id'];
            $res = $sq_banner->editBanner($where,$inser_array);

            if($res){
                showMessage("轮播图编辑成功","index.php?act=sq_setting&op=index");
            }else{
                showMessage("轮播图编辑失败");
            }
        }
        Tpl::output('banner',$tmp);
        Tpl::setDirquna('mobile');
        Tpl::showpage('sq_banner.edit');
    }

    /**
     * 删除
     */
    public function sq_setting_delOp(){
        if (intval($_GET['id']) > 0){
            $sq_banner = Model('sq_setting');
            /**
             * 删除图片
             */
            $tmp = $sq_banner->getBannerInfo(array('id'=>$_GET['id']));
            if (!empty($tmp['images'])){
                @unlink(BASE_ROOT_PATH.DS.DIR_UPLOAD.DS.ATTACH_MOBILE.'/sqbanner/'.$tmp['images']);
            }
            $sq_banner->del($tmp['id']);
            showMessage("删除成功",'index.php?act=sq_setting&op=index');
        }else {
            showMessage("请选择要删除的轮播图",'index.php?act=sq_setting&op=index');
        }
    }
}
